package pers.gxm.facade;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PageMaker {

    public static void main(String[] args) {
//        PageMaker.makeWelcomePage("hyuki@hyuki.com","welcome.html");
        PageMaker.makeLinkPage("linkpage1.html");
    }
    private PageMaker(){

    }
    public static void makeWelcomePage(String mailaddr,String filename){
        Properties maildata = Database.getProperties("maildata");
        String username = maildata.getProperty(mailaddr);


        try {
            HtmlWriter htmlWriter = new HtmlWriter(new FileWriter(filename));
            htmlWriter.title("Welcome to " + username + "'s page!");
            htmlWriter.paragraph("欢迎来到" + username + "的主页。");
            htmlWriter.paragraph("等着你的邮件哦！");
            htmlWriter.mailto(mailaddr,username);
            htmlWriter.close();
            System.out.println(filename + "is created for" + mailaddr + "(" + username + ")");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public static void makeLinkPage(String filename){
        Set<Map.Entry<Object, Object>> maildata = Database.getProperties("maildata").entrySet();
        Iterator<Map.Entry<Object, Object>> iterator = maildata.iterator();
        HtmlWriter htmlWriter;
        try {
            htmlWriter = new HtmlWriter(new FileWriter(filename)) ;
            htmlWriter.title("Link page");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        while(iterator.hasNext()){
            Map.Entry<Object, Object> entry = iterator.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            try {
                htmlWriter.mailto(key,value);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        }
        try {
            htmlWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("filename = " + filename + " is created");
    }
}
